﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PaDgame;

namespace PaDgame
{
	/*
	public class Moveable : MonoBehaviour
	{
		readonly float moveSpeed = 20;
		int moveStatus;
		Vector3 destination;

		void Update()
		{
			if (moveStatus == 1)
			{
				transform.position = Vector3.MoveTowards(transform.position, destination, moveSpeed * Time.deltaTime);
			}

			if (transform.position == destination)
			{
				moveStatus = 0;
			}
		}
		public void setDestination(Vector3 dest)
		{
			destination = dest;
			moveStatus = 1;
		}

		public void reset()
		{
			moveStatus = 0;
		}
	}
	*/

	public class MyCharacterController
	{
		readonly GameObject character;
		//readonly Moveable move;
		readonly ClickGUI click;
		readonly int characterType;
		readonly float moveSpeed = 20;

		bool isOnBoat;
		BankController bankController;

		public MyCharacterController(string characterName)
		{
			if (characterName == "priest")
			{
				character = Object.Instantiate(Resources.Load("Prefabs/priest", typeof(GameObject)), Vector3.zero, Quaternion.identity, null) as GameObject;
				characterType = 0;
			}
			else
			{
				character = Object.Instantiate(Resources.Load("Prefabs/devil", typeof(GameObject)), Vector3.zero, Quaternion.identity, null) as GameObject;
				characterType = 1;
			}
			//move = character.AddComponent(typeof(Moveable)) as Moveable;
			click = character.AddComponent(typeof(ClickGUI)) as ClickGUI;
			click.setController(this);
		}

		public void setName(string name)
		{
			character.name = name;
		}

		public void setPosition(Vector3 pos)
		{
			character.transform.position = pos;
		}
		/*
		public void moveToDestination(Vector3 destination)
		{
			move.setDestination(destination);
		}
		*/
		public int getType()
		{
			return characterType;
		}

		public string getName()
		{
			return character.name;
		}

		public void getOnBoat(BoatController boatCtrl)
		{
			character.transform.parent = boatCtrl.getBoat().transform;
			bankController = null;
			isOnBoat = true;
		}

		public void getOffBoat(BankController bankCtrl)
		{
			character.transform.parent = null;
			bankController = bankCtrl;
			isOnBoat = false;
		}

		public bool getIsOnBoat()
		{
			return isOnBoat;
		}

		public BankController getBankController()
		{
			return bankController;
		}

		public GameObject getCharacter()
		{
			return character;
		}

		public float getMoveSpeed()
		{
			return moveSpeed;
		}

		public void reset()
		{
			//move.reset();
			bankController = (SSDirector.getInstance().currentSceneController as FirstController).fromBank;
			getOffBoat(bankController);
			setPosition(bankController.getEmptyPosition());
			bankController.getOffBoat(this);
		}
	}
	
	public class BankController
	{
		readonly GameObject bank;
		readonly Vector3[] positions;
		readonly int toOrFrom;

		MyCharacterController[] passenger;

		public BankController(string bankName)
		{
			positions = new Vector3[] {new Vector3(-11F,1.25F,5F), new Vector3(-10F,1.25F,5F), new Vector3(-9F,1.25F,5F), new Vector3(-7F,1.25F,5F), new Vector3(-6F,1.25F,5F), new Vector3(-5F,1.25F,5F)};

			passenger = new MyCharacterController[6];

			if (bankName == "from")
			{
				bank = Object.Instantiate(Resources.Load("Prefabs/bank", typeof(GameObject)), new Vector3(-8F, 0.5F, 5F), Quaternion.identity, null) as GameObject;
				bank.name = "from";
				toOrFrom = 1;
			}
			else
			{
				bank = Object.Instantiate(Resources.Load("Prefabs/bank", typeof(GameObject)), new Vector3(8F, 0.5F, 5F), Quaternion.identity, null) as GameObject;
				bank.name = "to";
				toOrFrom = -1;
			}
		}

		public int getEmptyIndex()
		{
			for (int i = 0; i < passenger.Length; i++)
			{
				if (passenger[i] == null)
				{
					return i;
				}
			}
			return -1;
		}

		public int getToOrFrom()
		{
			return toOrFrom;
		}

		public Vector3 getEmptyPosition()
		{
			Vector3 pos = positions[getEmptyIndex()];
			pos.x *= toOrFrom;
			return pos;
		}

		public void getOffBoat(MyCharacterController chCtrl)
		{
			int index = getEmptyIndex();
			passenger[index] = chCtrl;
		}

		public MyCharacterController getOnBoat(string passengerName)
		{
			for (int i = 0; i < passenger.Length; i++)
			{
				if (passenger[i] != null && passenger[i].getName() == passengerName)
				{
					MyCharacterController chCtrl = passenger[i];
					passenger[i] = null;
					return chCtrl;
				}
			}
			return null;
		}

		public int[] getCharacterNum()
		{
			int[] count = { 0, 0 };
			for (int i = 0; i < passenger.Length; i++)
			{
				if (passenger[i] != null)
                {
					if (passenger[i].getType() == 0)
					{
						count[0]++;
					}
					else
					{
						count[1]++;
					}
				}
			}
			return count;
		}

		public void reset()
		{
			passenger = new MyCharacterController[6];
		}
	}
	
	public class BoatController
	{
		readonly GameObject boat;
		//readonly Moveable move;
		readonly Vector3 fromPosition = new Vector3(-3F, 0.75F, 5F);
		readonly Vector3 toPosition = new Vector3(3F, 0.75F, 5F);
		readonly Vector3[] fromPositions;
		readonly Vector3[] toPositions;
		readonly float moveSpeed = 20;

		int toOrFrom;
		MyCharacterController[] passenger;

		public BoatController()
		{
			boat = Object.Instantiate(Resources.Load("Prefabs/boat", typeof(GameObject)), fromPosition, Quaternion.identity, null) as GameObject;
			boat.name = "boat";
			//move = boat.AddComponent(typeof(Moveable)) as Moveable;
			fromPositions = new Vector3[] { new Vector3(-3.5F, 1.25F, 5F), new Vector3(-2.5F, 1.25F, 5F) };
			toPositions = new Vector3[] { new Vector3(2.5F, 1.25F, 5F), new Vector3(3.5F, 1.25F, 5F) };
			toOrFrom = 1;
			passenger = new MyCharacterController[2];
			boat.AddComponent(typeof(ClickGUI));
		}
		
		public bool isEmpty()
		{
			for (int i = 0; i < passenger.Length; i++)
			{
				if (passenger[i] != null)
				{
					return false;
				}
			}
			return true;
		}
		
		public int getEmptyIndex()
		{
			for (int i = 0; i < passenger.Length; i++)
			{
				if (passenger[i] == null)
				{
					return i;
				}
			}
			return -1;
		}

		/*
		 public void boatMove()
		{
			if (toOrFrom == -1)
			{
				move.setDestination(fromPosition);
				toOrFrom = 1;
			}
			else
			{
				move.setDestination(toPosition);
				toOrFrom = -1;
			}
		}
		 */

		public Vector3 getBoatDestination()
		{
			if (toOrFrom == -1)
			{
				//move.setDestination(fromPosition);
				toOrFrom = 1;
				return fromPosition;
			}
			else
			{
				//move.setDestination(toPosition);
				toOrFrom = -1;
				return toPosition;
			}
		}

		public Vector3 getEmptyPosition()
		{
			Vector3 pos;
			int emptyIndex = getEmptyIndex();
			if (toOrFrom == -1)
			{
				pos = toPositions[emptyIndex];
			}
			else
			{
				pos = fromPositions[emptyIndex];
			}
			return pos;
		}

		public void getOnBoat(MyCharacterController chCtrl)
		{
			int index = getEmptyIndex();
			passenger[index] = chCtrl;
		}

		public MyCharacterController getOffBoat(string passengerName)
		{
			for (int i = 0; i < passenger.Length; i++)
			{
				if (passenger[i] != null && passenger[i].getName() == passengerName)
				{
					MyCharacterController chCtrl = passenger[i];
					passenger[i] = null;
					return chCtrl;
				}
			}
			return null;
		}

		public GameObject getBoat()
		{
			return boat;
		}

		public int getToOrFrom()
		{
			return toOrFrom;
		}

		public int[] getCharacterNum()
		{
			int[] count = { 0, 0 };
			for (int i = 0; i < passenger.Length; i++)
			{
				if (passenger[i] == null)
					continue;
				if (passenger[i].getType() == 0)
				{
					count[0]++;
				}
				else
				{
					count[1]++;
				}
			}
			return count;
		}

		public float getMoveSpeed()
        {
			return moveSpeed;
        }

		public void reset()
		{
			//move.reset();
			if (toOrFrom == -1)
			{
				toOrFrom = 1;
			}
			boat.transform.position = fromPosition;
			passenger = new MyCharacterController[2];
		}
	}
}